﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mimoza.GUI;

namespace Mimoza.CitrixPlugin
{
    public partial class GeneralPage : Mimoza.GUI.WizardPage
    {
        private CitrixData m_data;
        public GeneralPage()
        {
            InitializeComponent();
        }

        override public void Init()
        {
            m_data = this.Wizard.UserData as CitrixData;

            textBoxSessionName.Text = m_data.SessionName;
            checkBoxInStartup.Checked = m_data.IsStartOnStartup;
            textBoxComputer.Text = m_data.Computer;
            textBoxUser.Text = m_data.UserName;
            textBoxDomain.Text = m_data.UserDomain;
            if (m_data.ServerType == ServerType.Server)
            {
                radioButtonServer.Checked = true;
            }
            else
            {
                radioButtonPubApp.Checked = true;
                textBoxPublishedApp.Text = m_data.PublishedAppName;
            }

            checkBoxSmartCard.Checked = m_data.UseSmartCardOnLogon;
            checkBoxSmartCard_CheckedChanged(null, null);

            this.IsConfigured = CheckSessionName(false) && CheckComputerName(false);

            this.checkBoxSmartCard.CheckedChanged += new System.EventHandler(this.checkBoxSmartCard_CheckedChanged);
            this.textBoxDomain.TextChanged += new System.EventHandler(this.textBoxDomain_TextChanged);
            this.radioButtonServer.CheckedChanged += new System.EventHandler(this.radioButtonServer_CheckedChanged);
            this.textBoxUser.TextChanged += new System.EventHandler(this.textBoxUser_TextChanged);
            this.textBoxComputer.TextChanged += new System.EventHandler(this.textBoxComputer_TextChanged);
            this.checkBoxInStartup.CheckedChanged += new System.EventHandler(this.checkBoxInStartup_CheckedChanged);
            this.textBoxSessionName.TextChanged += new System.EventHandler(this.textBoxSessionName_TextChanged);
        }

        override public bool Try2LeavePage(WizardPageLeavingDirection direction)
        {
            if (direction == WizardPageLeavingDirection.Back)
            {
                return true;
            }

            if (!CheckSessionName(true)) return false;
            if (!CheckComputerName(true)) return false;

            this.IsConfigured = true;
            m_data.SessionName = textBoxSessionName.Text;
            m_data.IsStartOnStartup = checkBoxInStartup.Checked;
            m_data.Computer = textBoxComputer.Text;
            m_data.UserName = textBoxUser.Text;
            m_data.UserDomain = textBoxDomain.Text;
            if (radioButtonServer.Checked) m_data.ServerType = ServerType.Server;
            else
            {
                m_data.ServerType = ServerType.PublishedApplication;
                m_data.PublishedAppName = textBoxPublishedApp.Text;
            }
            m_data.UseSmartCardOnLogon = checkBoxSmartCard.Checked;

            return true;
        }

        bool CheckSessionName(bool showMsg)
        {
            if (string.IsNullOrEmpty(textBoxSessionName.Text))
            {
                if (showMsg)
                {
                    MessageBox.Show(Resource.EmptySessionMessage, Resource.WrongParametersCaption,
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBoxSessionName.Focus();
                }
                return false;
            }
            return true;
        }

        bool CheckComputerName(bool showMsg)
        {
            if (string.IsNullOrEmpty(textBoxComputer.Text))
            {
                if (showMsg)
                {
                    MessageBox.Show(Resource.EmptyComputerNameMessage, Resource.WrongParametersCaption,
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBoxComputer.Focus();
                }
                return false;
            }
            return true;
        }

        private void textBoxComputer_TextChanged(object sender, EventArgs e)
        {
            if (this.textBoxComputer.Text != null && this.textBoxComputer.Text.Length > 0)
            {
                this.IsConfigured = true;
            }
        }

        private void checkBoxSmartCard_CheckedChanged(object sender, EventArgs e)
        {
            labelUser.Enabled = !checkBoxSmartCard.Checked;
            textBoxUser.Enabled = !checkBoxSmartCard.Checked;

            labelDomain.Enabled = !checkBoxSmartCard.Checked;
            textBoxDomain.Enabled = !checkBoxSmartCard.Checked;

            if (null != sender)
            {
                this.RaiseOnPageModified();
            }
        }
  
        private void textBoxSessionName_TextChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void checkBoxInStartup_CheckedChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void radioButtonServer_CheckedChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void textBoxUser_TextChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void textBoxDomain_TextChanged(object sender, EventArgs e)
        {
            this.RaiseOnPageModified();
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void radioButtonServer_CheckedChanged_1(object sender, EventArgs e)
        {
            radioButtonPubApp_CheckedChanged(null, null);
        }

        private void radioButtonPubApp_CheckedChanged(object sender, EventArgs e)
        {
            textBoxPublishedApp.Enabled = radioButtonPubApp.Checked;
        }
    }
}
